﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Reflection;
using System.Runtime.InteropServices;

namespace 提交管理平台V1
{
    public partial class frmMain : Form
    {
        UserInfo ui;
        SqlTools sqt;

        /// <summary>
        /// 复制粘贴功能所需的已选定单元格集合
        /// </summary>
        DataGridViewSelectedCellCollection DataGridClipBoard;

        /// <summary>
        /// 更新开票状态的查询脚本
        /// </summary>
        string strUpdateKp = "update @表名称 set 已开票='@已开票',开票日期=@开票日期 where 序号=@序号";

        /// <summary>
        /// 更新已完成状态的查询脚本
        /// </summary>
        string strUpdateYwc = "update @表名称 set 已完成='@已完成' where 序号=@序号";

        /// <summary>
        /// 默认查询脚本
        /// </summary>
        string strDefualtSelectString = string.Empty;

        /// <summary>
        /// 生成数据更新脚本用的查询脚本
        /// </summary>
        string strStructSelectString = string.Empty;

        public frmMain(UserInfo tmpui)
        {
            InitializeComponent();
            ui = tmpui;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            CheckUserSettings();
            iniInterface();
            if (!ui.UserSettings.FieldsNotSet)
            {
                iniComboBox();
                DoQuery();
            }
            
        }

        private void iniInterface()
        {
            //系统管理员
            //七专库操作员
            //一般品库操作员
            //特容厂库房操作员
            //包装检验组操作员
            //下厂验收资料打印员
            //微波库操作员
            //查询
            switch (ui.Character)
            {
                case "系统管理员":
                case "成品库操作员":
                //case "七专库操作员":
                //case "一般品库操作员":
                case "包装检验组操作员":
                case "下厂验收资料打印员":
                case "查询":
                default:
                    tsiStorageName.Text = "全部库房";
                    tsiStorageName.Enabled = true;
                    break;
                case "特容厂库房操作员":
                    tsiStorageName.Text = "601库";
                    tsiStorageName.Enabled = false ;
                    tsiTableName.Enabled = false;
                    break;
                case "微波库房操作员":
                    tsiStorageName.Text = "微波库";
                    tsiStorageName.Enabled = false;
                    tsiTableName.Enabled = false;
                    break;
            }
            #region 菜单
            //字段设置
            tsmiFieldSetting .Visible = ui.CheckPermission("设置/字段设置");
            //包装数量设置
            tsmiPackQuantitySetting.Visible = ui.CheckPermission("设置/包装数量设置");
            //打印机设置
            tsmiPrinterSetting.Visible = ui.CheckPermission("设置/打印机设置");
            //用户管理
            tsmiUserManage.Visible = ui.CheckPermission("设置/用户管理");
            //权限设置
            tsmiPermissionConfig.Visible = ui.CheckPermission("设置/权限设置");
            //权限设置
            tsmiChangePassword.Visible = ui.CheckPermission("设置/修改密码");
            #endregion
            #region 按钮
            //添加
            tsbtnAdd.Visible = ui.CheckPermission("数据/新增");
            //删除
            tsbtnRemove.Visible = ui.CheckPermission("数据/删除");
            //编辑
            tsbtnEdit.Visible = ui.CheckPermission("数据/编辑");
            //保存
            tsbtnSave.Visible = ui.CheckPermission("数据/保存");
            //取消
            tsbtnCancel.Visible = ui.CheckPermission("数据/取消");
            //已开票
            tsbtnConfirm_ykp.Visible = ui.CheckPermission("数据/已开票");
            //未开票
            tsbtnConfirm_wkp.Visible = ui.CheckPermission("数据/未开票");
            //已完成
            tsbtnConfirm_ywc.Visible = ui.CheckPermission("数据/已完成");
            //未完成
            tsbtnConfirm_wwc.Visible = ui.CheckPermission("数据/未完成");
            //打印标签
            tsbtnPrint_bq.Visible = ui.CheckPermission("打印/标签");
            //打印发票
            tsbtnPrint_fp.Visible = ui.CheckPermission("打印/发票");
            //打印产品清单
            tsbtnPrint_qd.Visible = ui.CheckPermission("打印/清单");
            //打印产品资料
            tsbtnPrint_zl.Visible = ui.CheckPermission("打印/资料");
            //导出为Excel
            tsbtnExport.Visible = ui.CheckPermission("打印/导出为Excel");
            #endregion
        }

        private void CheckUserSettings()
        {
            bool bQuit = false;
            while (ui.UserSettings.FieldsNotSet)
            {
                if (MessageBox.Show("字段或最大记录条数没有设置，点击确定进入设置窗口或点击取消退出软件！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    frmConfig newFrmConfig = new frmConfig(ui,"QZKFC");
                    newFrmConfig.StartPosition = FormStartPosition.CenterParent;
                    newFrmConfig.ShowDialog();
                    ui.iniUserSettings();
                }
                else
                {
                    GeneralTools.ShowInfoMsg("软件将退出，请联系管理员！");
                    bQuit = true;
                    break;
                }
            }
            if (bQuit)
            {
                Application.ExitThread();
            }
        }

        private void iniComboBox()
        {
            cmbFields.Items.Clear();
            cmbFields_OrderBy.Items.Clear();
            for (int i = 0; i < ui.UserSettings.Fields.Length; i++)
            {
                cmbFields.Items.Add(ui.UserSettings.Fields[i]);
                cmbFields_OrderBy.Items.Add(ui.UserSettings.Fields[i]);
            }
            if (cmbFields.Items.Count > 0)
            {
                cmbFields.SelectedIndex = 0;
                cmbFields_OrderBy.SelectedIndex = 0;
            }
            cmbCompute.SelectedIndex = 0;
            cmbOrder.SelectedIndex = 1;
        }

        private void DoQuery()
        {
            //获取用户字段设置
            List<string> strTmpFields = new List<string>(ui.UserSettings.Fields);
            //获取要查询的表的表名称
            string strTmpTableName = string.Empty;
            switch (tsiTableName.Text)
            {
                case "2008年":
                    strTmpTableName = "QZKFC_2008";
                    strTmpFields.Remove("客户名称");
                    break;
                case "2009年":
                    strTmpTableName = "QZKFC_2009";
                    strTmpFields.Remove("客户名称");
                    break;
                case "2009年之后":
                    strTmpTableName = "QZKFC";
                    break;
            }
            //选择字段中是否包含客户名称
            bool bContainCustomerName = false;
            //验证字段是否存在
            SqlTools.DataStruct ds = sqt.GetTableStruct(strTmpTableName);
            for(int i=strTmpFields .Count -1;i>=0;i--)
            {
                if (strTmpFields[i].Equals("客户名称"))
                {
                    bContainCustomerName = true;
                    continue;
                }
                if (ds.FindField(strTmpFields[i]) < 0)
                {
                    strTmpFields.RemoveAt(i);
                }
            }
            if (strTmpFields.Count == 0)
            {
                GeneralTools.ShowInfoMsg("没有选择有效的字段！请重新设置字段，再进行查询！");
                return;
            }
            //连接查询语句
            string strTmpSelectString = string.Empty;
            string strTmpStructString = string.Empty;
            string strTmpSelectCount=string .Empty ;
            strTmpSelectString += "select top ";
            strTmpStructString += "select ";
            strTmpSelectCount +="select count(*) ";
            //最大记录条数
            strTmpSelectString += ui.UserSettings.MaxRecordCount.ToString()+" ";
            //查询字段
            for (int i = 0; i < strTmpFields .Count ; i++)
            {
                if (strTmpFields [i].Equals ("客户名称"))
                {
                    strTmpSelectString += "hta004 客户名称";
                }
                else
                {
                    strTmpSelectString += strTmpFields[i];
                    strTmpStructString += strTmpFields[i];
                }
                if (i < strTmpFields .Count  - 1)
                {
                    strTmpSelectString += ",";
                    if (!strTmpFields[i].Equals("客户名称"))
                    {
                        strTmpStructString += ",";
                    }
                }
            }
            if(strTmpStructString .EndsWith (","))
            {
                strTmpStructString = strTmpStructString.Substring (0, strTmpStructString.Length - 1);
            }
            //表名称
            if (bContainCustomerName)
            {
                strTmpSelectString += " from " + strTmpTableName + " left join TPAMain.dbo.hta_table on 订单编号 collate Chinese_PRC_BIN=hta001";
                strTmpSelectCount += " from " + strTmpTableName + " left join TPAMain.dbo.hta_table on 订单编号 collate Chinese_PRC_BIN=hta001";
            }
            else
            {
                strTmpSelectString += " from " + strTmpTableName;
                strTmpSelectCount += " from " + strTmpTableName;
            }
            strTmpStructString +=" from " + strTmpTableName;
            strDefualtSelectString = strTmpSelectString;
            strStructSelectString = strTmpStructString;
            //查询条件
            if (lstCondition.Items.Count > 0)
            {
                for (int i = 0; i < lstCondition.Items.Count; i++)
                {
                    if (strTmpSelectString .Contains (" where "))
                    {
                        strTmpSelectString +=" and ";
                        strTmpSelectCount +=" and ";
                    }
                    else 
                    {
                        strTmpSelectString +=" where ";
                        strTmpSelectCount +=" where ";
                    }
                    string strTmpCondition = lstCondition.Items[i].ToString();
                    string strTmpKey1 = string.Empty;     //字段
                    string strTmpKey2 = string.Empty;     //计算符
                    string strTmpKey3 = string.Empty;     //值1
                    string strTmpKey4 = string.Empty;     //值2
                    if (strTmpCondition.StartsWith("$"))
                    {
                        strTmpKey1 = strTmpCondition.Substring(1).Split(new string[] { "　" }, StringSplitOptions.RemoveEmptyEntries).GetValue(0).ToString();
                        strTmpKey2 = strTmpCondition.Substring(1).Split(new string[] { "　" }, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString();
                        strTmpKey3 = strTmpCondition.Substring(1).Split(new string[] { "　" }, StringSplitOptions.RemoveEmptyEntries).GetValue(2).ToString();
                        switch (strTmpKey2)
                        {
                            case "模糊等于":
                                strTmpKey2 = "like";
                                break;
                            case "大于":
                                strTmpKey2 = ">";
                                break;
                            case "大于等于":
                                strTmpKey2 = ">=";
                                break;
                            case "等于":
                                strTmpKey2 = "=";
                                break;
                            case "小于":
                                strTmpKey2 = "<";
                                break;
                            case "小于等于":
                                strTmpKey2 = "<=";
                                break;
                        }
                        if (strTmpKey1.Equals("客户名称"))
                        {
                            strTmpSelectString += "hta004 " + strTmpKey2 + " '" + strTmpKey3 + "'";
                            strTmpSelectCount += "hta004 " + strTmpKey2 + " '" + strTmpKey3 + "'";
                        }
                        else
                        {
                            strTmpSelectString += strTmpKey1 + " " + strTmpKey2 + " '" + strTmpKey3 + "'";
                            strTmpSelectCount += strTmpKey1 + " " + strTmpKey2 + " '" + strTmpKey3 + "'";
                        }
                    }
                    else
                    {
                        strTmpKey1 = strTmpCondition.Substring(1).Split(new string[] { "　" }, StringSplitOptions.RemoveEmptyEntries).GetValue(0).ToString();
                        strTmpKey2 = strTmpCondition.Substring(1).Split(new string[] { "　" }, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString();
                        strTmpKey3 = strTmpCondition.Substring(1).Split(new string[] { "　" }, StringSplitOptions.RemoveEmptyEntries).GetValue(2).ToString().Split(new string[] { "至" }, StringSplitOptions.RemoveEmptyEntries).GetValue(0).ToString();
                        strTmpKey4 = strTmpCondition.Substring(1).Split(new string[] { "　" }, StringSplitOptions.RemoveEmptyEntries).GetValue(2).ToString().Split(new string[] { "至" }, StringSplitOptions.RemoveEmptyEntries).GetValue(1).ToString();
                        strTmpSelectString += strTmpKey1 + " between '" + strTmpKey3 + "' and '"+strTmpKey4 +"'";
                        strTmpSelectCount += strTmpKey1 + " between '" + strTmpKey3 + "' and '"+strTmpKey4 +"'";
                    }
                }
            }
            //未开票
            if (chkWKP.Checked )
            {
                if (strTmpSelectString.Contains(" where "))
                {
                    strTmpSelectString += " and ";
                    strTmpSelectCount += " and ";
                }
                else
                {
                    strTmpSelectString += " where ";
                    strTmpSelectCount += " where ";
                }
                strTmpSelectString += "(已开票='未开票' or 已开票='' or 已开票 is null)";
                strTmpSelectCount += "(已开票='未开票' or 已开票='' or 已开票 is null)";
            }
            //未备资料
            if (chkWBZL.Checked)
            {
                if (strTmpSelectString.Contains(" where "))
                {
                    strTmpSelectString += " and ";
                    strTmpSelectCount += " and ";
                }
                else
                {
                    strTmpSelectString += " where ";
                    strTmpSelectCount += " where ";
                }
                strTmpSelectString += "(已备资料='未备资料' or 已备资料='' or 已备资料 is null)";
                strTmpSelectCount += "(已备资料='未备资料' or 已备资料='' or 已备资料 is null)";
            }
            //库房
            if (!tsiStorageName.Text.Equals("全部库房"))
            {
                if (strTmpSelectString.Contains(" where "))
                {
                    strTmpSelectString += " and ";
                    strTmpSelectCount += " and ";
                }
                else
                {
                    strTmpSelectString += " where ";
                    strTmpSelectCount += " where ";
                }
                strTmpSelectString += "提交库房='" + tsiStorageName .Text  + "'";
                strTmpSelectCount += "提交库房='" + tsiStorageName.Text + "'";
            }
            //排序
            if (cmbOrder.SelectedIndex == 0)
            {
                if (cmbFields_OrderBy.Text.Equals("客户名称"))
                {
                    strTmpSelectString += " order by hta004 asc";
                }
                else
                {
                    strTmpSelectString += " order by " + cmbFields_OrderBy.Text + " asc";
                }
            }
            else 
            {
                if (cmbFields_OrderBy.Text.Equals("客户名称"))
                {
                    strTmpSelectString += " order by hta004 desc";
                }
                else
                {
                    strTmpSelectString += " order by " + cmbFields_OrderBy.Text + " desc";
                }
            }
            //获取数据并绑定到dgv1
            dgv1 .DataSource =sqt.GetData (strTmpSelectString );
            dgv1 .AutoResizeColumns ();
            dgv1.Font =new Font ("雅黑", (float )ui.UserSettings.FontSize);
            int iTmpRecordCount=GeneralTools .GetInt (sqt.DoCmdScalarData (strTmpSelectCount ),0);
            tsiTotalRecordCount .Text = "当前查询记录总数"+iTmpRecordCount .ToString ()+"条，仅显示前"+ui.UserSettings .MaxRecordCount .ToString ()+"条";
        }

        private void RefreshDataGrid()
        {
            if (dgv1.CurrentCell != null)
            {
                int X = dgv1.CurrentCell.RowIndex;
                int Y = dgv1.CurrentCell.ColumnIndex;
                DoQuery();
                if (X<dgv1.Rows.Count &&Y<dgv1.Columns.Count)
                {
                    dgv1.CurrentCell = dgv1.Rows[X].Cells[Y];
                }
            }
            
        }

        /// <summary>
        /// 提交数据表修改
        /// </summary>
        /// <returns>修改是否成功</returns>
        private bool UpdateChanges()
        {
            DataTable tmpdt = ((DataTable)dgv1.DataSource).GetChanges();
            if (tmpdt != null)
            {
                return sqt.UpdateChanges(strStructSelectString , tmpdt);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 检查字段是否在当前查询结果中出现
        /// </summary>
        /// <param name="strFieldToCheck">要检查的字段，字段以"|"分隔</param>
        /// <returns>是否存在</returns>
        private bool CheckFieldExist(string strFieldToCheck)
        {
            bool bFieldExist = true;
            string[] strTmpFields = strFieldToCheck.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < strTmpFields.Length; i++)
            {
                if (dgv1.Columns[strTmpFields[i]] == null)
                {
                    bFieldExist = false;
                    GeneralTools.ShowInfoMsg("字段'" + strTmpFields[i] + "'不存在！");
                }
            }
            return bFieldExist;
        }

        /// <summary>
        /// 读取指定订单的备注
        /// </summary>
        /// <param name="strContractNo">订单编号</param>
        /// <returns>订单备注</returns>
        private string ReadContractMemo(string strContractNo)
        {
            if (strContractNo.Trim().Length == 0)
            {
                return string.Empty;
            }
            string strMemo = string.Empty;
            strMemo = GeneralTools.GetString(sqt.DoCmdScalarData("select htc002 from TPAMain.dbo.htc_table where htc001='" + strContractNo + "'"), string.Empty);
            return strMemo;
        }

        private DataTable GetReportData(DataGridViewSelectedRowCollection dgsr)
        {
            DataTable tmpdt = ((DataTable)dgv1.DataSource).Clone();
            DataRow r;
            for (int i = dgsr.Count - 1; i >= 0; i--)
            {
                r = tmpdt.NewRow();
                for (int k = 0; k < tmpdt.Columns.Count; k++)
                {
                    r[k] = dgsr[i].Cells[k].Value;
                }
                tmpdt.Rows.Add(r);
            }
            return (tmpdt);
        }

        private void epQuery_Resize(object sender, EventArgs e)
        {
            frmMain_Resize(this, new EventArgs());
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            //pOprationBar.Top = epQuery.Top + epQuery.Height + 2;
            //dgv1.Top = pOprationBar.Top + pOprationBar.Height + 2;
            //dgv1.Height = statusstrip1.Top - 2 - dgv1.Top;
            //dgv1.Width = this.Width -20;
        }

        private void cmbCompute_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbCompute.SelectedIndex == 6)
            {
                pSingleValue.Visible = false ;
                pValueRange.Visible = true;
            }
            else
            {
                pSingleValue.Visible = true;
                pValueRange.Visible = false;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cmbFields.Text.Equals(string.Empty))
            {
                cmbFields.Focus();
                return;
            }
            if (cmbCompute.SelectedIndex < 6)
            {
                if (txtValue.Text.Trim().Equals(string.Empty))
                {
                    txtValue.Focus();
                    return;
                }
            }
            else
            {
                if (txtFrom.Text.Trim().Equals(string.Empty))
                {
                    txtFrom .Focus ();
                    return ;
                }
                if (txtTo.Text.Trim().Equals(string.Empty))
                {
                    txtTo .Focus ();
                    return ;
                }
            }
            string strTmpField = cmbFields.Text;
            string strKeyword=string.Empty ;
            if (cmbCompute .SelectedIndex <6)
            {
                strKeyword = "$" + strTmpField + "　" + cmbCompute.Text + "　";
            }
            else 
            {
                strKeyword = "#" + strTmpField + "　由";
            }
            bool bItemExist = false;
            if (lstCondition.Items.Count > 0)
            {
                for (int i = 0; i < lstCondition.Items.Count; i++)
                {
                    if (GeneralTools.GetString(lstCondition.Items[i], string.Empty).StartsWith(strKeyword))
                    {
                        if (cmbCompute.SelectedIndex < 6)
                        {
                            lstCondition.Items[i] = strKeyword + "　" + txtValue.Text;
                        }
                        else
                        {
                            lstCondition.Items[i] = strKeyword + "　" + txtFrom.Text + "至"+txtTo .Text ;
                        }
                        bItemExist = true;
                        break;
                    }
                }
            }
            if (!bItemExist)
            {
                if (cmbCompute.SelectedIndex < 6)
                {
                    lstCondition.Items.Add(strKeyword + "　" + txtValue.Text);
                }
                else
                {
                    lstCondition.Items.Add(strKeyword + "　" + txtFrom.Text + "至" + txtTo.Text);
                }
            }
            if (chkAutoQuery.Checked )
            {
                DoQuery();
            }
        }

        private void tsiTableName_2008_Click(object sender, EventArgs e)
        {
            tsiTableName.Text = "2008年";
            if (chkAutoQuery.Checked )
            {
                DoQuery();
            }
        }

        private void tsiTableName_2009_Click(object sender, EventArgs e)
        {
            tsiTableName.Text = "2009年";
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void tsiTableName_2010_Click(object sender, EventArgs e)
        {
            tsiTableName.Text = "2009年之后";
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DoQuery();
        }

        private void dgv1_SelectionChanged(object sender, EventArgs e)
        {
            tsiSelectedRecordCount.Text = "已选中" + dgv1.SelectedRows.Count.ToString() + "条";
        }

        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            DataTable tmpdt = (DataTable)dgv1.DataSource;
            if (tmpdt == null)
            {
                return;
            }
            DataRow r = tmpdt.NewRow();
            if (tmpdt.Columns["提交时间"] != null)
            {
                r["提交时间"] = DateTime.Now.Date.ToString("yyyy-MM-dd");
            }
            if (tsiStorageName.Text.Equals("全部库房"))
            {
                GeneralTools.ShowInfoMsg("请先选择库房！");
                return;
            }
            if (tmpdt.Columns["提交库房"] != null)
            {
                r["提交库房"] = tsiStorageName.Text;
            }
            else
            {
                GeneralTools.ShowInfoMsg("字段设置错误，请在字段设置中选择“提交库房”字段，否则将无法添加记录！");
                return;
            }
            tmpdt.Rows.Add(r);
            dgv1.CurrentCell = dgv1.Rows[dgv1 .Rows.Count -1].Cells["合同号"];
            tsbtnEdit_Click(this, new EventArgs());
            tsbtnSave.Enabled = true;
        }

        private void tsbtnRemove_Click(object sender, EventArgs e)
        {
            dgv1.EndEdit();
            if (dgv1.Columns["序号"] == null)
            {
                GeneralTools.ShowInfoMsg("必须在字段设置中选择序号列后才可删除行！");
                return;
            }
            if (dgv1.SelectedRows.Count > 0)
            {
                string msg = "你确定要删除序号为";
                for (int i = dgv1.SelectedRows.Count - 1; i >= 0; i--)
                {
                    msg += dgv1.SelectedRows[i].Cells["序号"].Value.ToString().Trim();
                    if (i > 0)
                    {
                        msg += ",";
                    }
                }
                msg += "的行吗？删除后将无法恢复！！！";
                DialogResult dr = MessageBox.Show(msg, "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    try
                    {
                        foreach (DataGridViewRow dgr in dgv1.SelectedRows)
                        {
                            dgv1.Rows.Remove(dgr);
                        }
                        tsbtnSave.Enabled = true;
                        tsbtnCancel.Enabled = true;
                    }
                    catch
                    {
                        GeneralTools.ShowErrorMsg("删除记录时出现错误，请检查后重新操作！");
                    }
                }
            }
        }

        private void tsbtnEdit_Click(object sender, EventArgs e)
        {
            if (dgv1.Columns.Count == 0)
            {
                return;
            }
            dgv1.ReadOnly = false;
            if (dgv1.CurrentCell == null)
            {
                if (dgv1.Rows.Count > 0)
                {
                    dgv1.CurrentCell = dgv1.Rows[0].Cells[1];
                }
                else
                {
                    return;
                }
            }
            if (dgv1.Columns["序号"] != null)
            {
                dgv1.Columns["序号"].ReadOnly = true;
            }
            if (dgv1.Columns["修改人"] != null)
            {
                dgv1.Columns["修改人"].ReadOnly = true;
            }
            if (dgv1.Columns["修改时间"] != null)
            {
                dgv1.Columns["修改时间"].ReadOnly = true;
            }
            dgv1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgv1.BeginEdit(true);
            tsbtnEdit.Enabled = false;
            tsbtnSave.Enabled = true;
            tsbtnCancel.Enabled = true;
            btnQuery.Enabled = false;
            btnClear.Enabled = false;
            dgv1.ContextMenuStrip  = contextMenuStrip1;
        }

        private void tsbtnCancel_Click(object sender, EventArgs e)
        {
            ((DataTable)dgv1.DataSource).RejectChanges();
            dgv1.SelectionMode = DataGridViewSelectionMode.FullRowSelect ;
            tsbtnRefresh_Click(this, new EventArgs());
            tsbtnEdit.Enabled = true;
            tsbtnSave.Enabled = false;
            tsbtnCancel.Enabled = false;
            btnQuery.Enabled = true;
            btnClear.Enabled = true;
            dgv1.ContextMenu = null;
            dgv1.ReadOnly = true;
            dgv1.ContextMenuStrip = null ;
        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            dgv1.EndEdit();
            DataTable tmpdt=(DataTable )dgv1 .DataSource ;
            if (tmpdt ==null )
            {
                return ;
            }
            
            UpdateChanges();
            #region 更新修改记录
            DataTable tmpdt2 = tmpdt.GetChanges(DataRowState.Modified);
            if (tmpdt2 != null && tmpdt2.Rows.Count > 0)
            {
                string strTmpTableName = string.Empty;
                switch (tsiTableName.Text)
                {
                    case "2008年":
                        strTmpTableName = " QZKFC_2008";
                        break;
                    case "2009年":
                        strTmpTableName = "QZKFC_2009";
                        break;
                    case "2009年之后":
                        strTmpTableName = "QZKFC";
                        break;
                }
                for (int i = 0; i < tmpdt2.Rows.Count; i++)
                {
                    sqt.DoCmdExecuteNonQuery("update " + strTmpTableName + " set 修改人='" + ui.UserName + "',修改时间=GetDate() where 序号=" + GeneralTools.GetString(tmpdt2.Rows[i]["序号"], string.Empty));
                }
            }
            #endregion
            dgv1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            tsbtnCancel.Enabled = false;
            tsbtnSave.Enabled = false;
            tsbtnEdit.Enabled = true;
            dgv1.ContextMenu = null;
            RefreshDataGrid();
            dgv1.ReadOnly = true;
            dgv1.ContextMenuStrip = null ;
        }

        private void tsbtnRefresh_Click(object sender, EventArgs e)
        {
            DoQuery();
        }

        private void tsbtnAutoResize_Click(object sender, EventArgs e)
        {
            dgv1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
            //Application.DoEvents();
            //GeneralTools.ShowWarningMsg("此合同不是614所二筛协议合同，请检查后重新操作！");
        }

        private void tsbtnPrint_fp_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                //检查字段是否存在
                if (!CheckFieldExist("合同号|质量等级|执行标准|附加协议|验收方式|订单编号|订单项次|单价|DPA数量|提交数量|批号|备注|用户计划号"))
                {
                    GeneralTools.ShowInfoMsg("请重新设置字段后再进行操作！");
                    return;
                }
                //检查提交信息中执行标准是否与订单一致
                string strSelectString = "select 序号 from QZKFC ,TPAMain .dbo .htb_table where 订单编号 collate Chinese_PRC_BIN=htb001 and 订单项次=htb002 and 执行标准 collate Chinese_PRC_BIN<>htb021 and (";
                for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                {
                    strSelectString += " 序号=" + dgv1.SelectedRows[i].Cells["序号"].Value.ToString();
                    if (i < dgv1.SelectedRows.Count - 1)
                    {
                        strSelectString += " or ";
                    }
                }
                strSelectString += ")";
                DataTable tmpdt = sqt.GetData(strSelectString);
                if (tmpdt.Rows.Count > 0)
                {
                    string strTmpMsg = "序号为：";
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        strTmpMsg += tmpdt.Rows[i]["序号"].ToString();
                        if (i < tmpdt.Rows.Count - 1)
                        {
                            strTmpMsg += ";";
                        }
                    }
                    strTmpMsg += "的记录，执行标准与订单中执行标准不一致，请检查！继续操作请点击确定，中断操作请点击取消。";
                    if (MessageBox.Show(strTmpMsg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                    {
                        return;
                    }
                }
                //如果订单编号不为空，检查客户是否为019-5所，如果是，弹出提示，要求单独处理
                bool b019_5 = false;
                if (dgv1.SelectedRows[0].Cells["订单编号"].Value != DBNull.Value &&
                    dgv1.SelectedRows[0].Cells["订单编号"].Value.ToString().Length == 12 &&
                    dgv1.SelectedRows[0].Cells["订单项次"].Value.ToString() != "")
                {
                    string strSelectUserName = "select hta003 客户编号,hta004 客户名称 from TPAMain.dbo.hta_table where hta001='@订单编号'";
                    DataTable dtUserInfo = sqt.GetData(strSelectUserName.Replace("@订单编号", dgv1.SelectedRows[0].Cells["订单编号"].Value.ToString()));
                    #region 检查用户是否是019-5所，如果是的话，显示提示信息
                    if (dtUserInfo != null && dtUserInfo.Rows.Count > 0 &&GeneralTools.GetString (dtUserInfo.Rows[0]["客户编号"],string.Empty ).StartsWith("019-5"))
                    {
                        MessageBox.Show (dgv1.SelectedRows[0].Cells["合同号"].Value.ToString ()+ "是019 - 5所产品，需要打印指定二维码标签，请单独处理该合同！");
                        b019_5 = true;
                    }
                    #endregion
                }
                string 购货单位, 生产单位, 制票人, 附件备注,开票形式;
                string tmpstr = dgv1.SelectedRows[0].Cells["合同号"].Value.ToString();
                if (tmpstr.Trim().Length > 0)
                {
                    购货单位 = tmpstr.Substring(2, 1);
                }
                else
                {
                    购货单位 = "";
                }
                if (ui.Character.Contains("特容厂"))
                {
                    生产单位 = "601";
                }
                else if (ui.Character.Contains("微波"))
                {
                    生产单位 = "微波事业部";
                }
                else 
                {
                    生产单位 = "603";
                }
                制票人 = ui.GetUserSetting("开票人");
                开票形式 = GetKPXS(dgv1.SelectedRows[0].Cells["订单编号"].Value.ToString());
                if (dgv1.SelectedRows[0].Cells["附加协议"].Value.ToString().Trim().Equals(string.Empty))
                {
                    if (b019_5)
                    {
                        附件备注 = "019-5    "+开票形式 + "        " + dgv1.SelectedRows[0].Cells["执行标准"].Value.ToString() + "        " + dgv1.SelectedRows[0].Cells["附加协议"].Value.ToString() + "        " + ReadContractMemo(dgv1.CurrentRow.Cells["订单编号"].Value.ToString());
                    }
                    else
                    {
                        附件备注 = 开票形式 + "        " + dgv1.SelectedRows[0].Cells["执行标准"].Value.ToString() + "        " + dgv1.SelectedRows[0].Cells["附加协议"].Value.ToString() + "        " + ReadContractMemo(dgv1.CurrentRow.Cells["订单编号"].Value.ToString());
                    }
                }
                else
                {
                    if (b019_5)
                    {
                        附件备注 = "019-5    " + 开票形式 + "        " + dgv1.SelectedRows[0].Cells["执行标准"].Value.ToString() + "        " + dgv1.SelectedRows[0].Cells["附加协议"].Value.ToString() + "        " + dgv1.SelectedRows[0].Cells["验收方式"].Value.ToString() + "        " + ReadContractMemo(dgv1.CurrentRow.Cells["订单编号"].Value.ToString());
                    }
                    else
                    {
                        附件备注 = 开票形式 + "        " + dgv1.SelectedRows[0].Cells["执行标准"].Value.ToString() + "        " + dgv1.SelectedRows[0].Cells["附加协议"].Value.ToString() + "        " + dgv1.SelectedRows[0].Cells["验收方式"].Value.ToString() + "        " + ReadContractMemo(dgv1.CurrentRow.Cells["订单编号"].Value.ToString());
                    }
                }
                OtherInfoFrm newotherinfoFrm = new OtherInfoFrm(购货单位, 生产单位, 制票人, 附件备注, GetReportData(dgv1.SelectedRows));
                newotherinfoFrm.ShowDialog();
            }
        }

        private string GetKPXS(string strOrderID)
        {
            SqlTools tmpsqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "TPAMain"));
            string strSelectString = "select hta020 from hta_table where hta001 ='" + strOrderID + "'";
            return GeneralTools.GetString(tmpsqt.DoCmdScalarData(strSelectString), string.Empty);
        }

        private void tsbtnPrint_bq_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                //检查字段是否存在
                if (!CheckFieldExist("合同号|执行标准|附加协议|质量等级|型号|尺寸|特性|电压|容量|引出端形式|提交数量|批号|淘汰率|筛选数量|生产日期|包装员|编带|订单编号"))
                {
                    GeneralTools.ShowInfoMsg("请重新设置字段后再进行操作！");
                    return;
                }
                //检查提交信息中执行标准是否与订单一致
                string strSelectString = "select 序号 from QZKFC ,TPAMain .dbo .htb_table where 订单编号 collate Chinese_PRC_BIN=htb001 and 订单项次=htb002 and 执行标准 collate Chinese_PRC_BIN<>htb021 and (";
                for(int i=0;i<dgv1 .SelectedRows.Count;i++)
                {
                    strSelectString += " 序号=" + dgv1.SelectedRows[i].Cells["序号"].Value.ToString();
                    if (i<dgv1 .SelectedRows .Count -1)
                    {
                        strSelectString += " or ";
                    }
                    //当片式产品备注中包含2.5UR时，调用函数校验是否符合2.5UR容量范围条件
                    if (dgv1.SelectedRows[i].Cells["备注"].Value.ToString().Contains ("2.5UR")&&
                        dgv1.SelectedRows[i].Cells["型号"].Value.ToString().Contains("41L")&&
                        (!dgv1.SelectedRows[i].Cells["质量等级"].Value.ToString().Equals ("CC"))&&
                        (!dgv1.SelectedRows[i].Cells["质量等级"].Value.ToString().Equals("CB")))
                    {
                        if (!CheckJsSxVoltage(dgv1.SelectedRows[i].Cells["型号"].Value.ToString(),
                            dgv1.SelectedRows[i].Cells["尺寸"].Value.ToString(),
                            dgv1.SelectedRows[i].Cells["电压"].Value.ToString(),
                            dgv1.SelectedRows[i].Cells["容量"].Value.ToString(),
                            dgv1.SelectedRows[i].Cells["批号"].Value.ToString()))
                        {
                            if(MessageBox.Show ("产品型号规格不符合2.5UR加速筛选条件，请检查确认后重新操作！","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.Cancel )
                                return;
                        }
                    }
                }
                strSelectString += ")";
                DataTable tmpdt = sqt.GetData(strSelectString);
                if (tmpdt .Rows.Count >0)
                {
                    string strTmpMsg = "序号为：";
                    for(int i=0;i<tmpdt.Rows.Count;i++)
                    {
                        strTmpMsg += tmpdt.Rows[i]["序号"].ToString();
                        if (i<tmpdt.Rows.Count -1)
                        {
                            strTmpMsg += ";";
                        }
                    }
                    strTmpMsg += "的记录，执行标准与订单中执行标准不一致，请检查！继续操作请点击确定，中断操作请点击取消。";
                    if (MessageBox .Show (strTmpMsg ,"提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)==DialogResult.Cancel)
                    {
                        return;
                    }
                }
                frmLabelPrint newFrmLabelPrint = new frmLabelPrint(GetReportData(dgv1.SelectedRows),ui);
                newFrmLabelPrint.ShowDialog();
            }
        }

        private void tsmiFieldSetting_Click(object sender, EventArgs e)
        {
            string strTmpTableName = string.Empty;
            switch (tsiTableName.Text)
            {
                case "2008年":
                    strTmpTableName = " QZKFC_2008";
                    break;
                case "2009年":
                    strTmpTableName = "QZKFC_2009";
                    break;
                case "2009年之后":
                    strTmpTableName = "QZKFC";
                    break;
            }
            frmConfig newFrmConfig = new frmConfig(ui, strTmpTableName);
            newFrmConfig.ShowDialog();
            ui.iniUserSettings();
            DoQuery();
        }

        private void tsmiPackQuantitySetting_Click(object sender, EventArgs e)
        {
            frmPackingQuantityManage newFrmPackingQuantityManage = new frmPackingQuantityManage(ui);
            newFrmPackingQuantityManage.ShowDialog();
        }

        private void tsmiPrinterSetting_Click(object sender, EventArgs e)
        {
            PrinterSettingsFrm newPrinterSettingsFrm = new PrinterSettingsFrm();
            newPrinterSettingsFrm.ShowDialog();
        }

        private void tsiStorageName_All_Click(object sender, EventArgs e)
        {
            tsiStorageName.Text = "全部库房";
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void tsiStorageName_wb_Click(object sender, EventArgs e)
        {
            tsiStorageName.Text = "微波库";
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void tsiStorageName_ybp_Click(object sender, EventArgs e)
        {
            tsiStorageName.Text = "一般品库";
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void tsiStorageName_qz_Click(object sender, EventArgs e)
        {
            tsiStorageName.Text = "七专库";
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void tsiStorageName_601_Click(object sender, EventArgs e)
        {
            tsiStorageName.Text = "601库";
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lstCondition.Items.Clear();
            DoQuery();
        }

        private void chkWKP_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void chkWBZL_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAutoQuery.Checked)
            {
                DoQuery();
            }
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.ExitThread();
        }

        private void dgv1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            string strHideFields = ui.GetUserSetting("隐藏字段设置");
            if (strHideFields.Contains("|" + e.Column.Name) || strHideFields.Contains(e.Column.Name + "|"))
            {
                e.Column.Visible = false;
            }
        }

        private void tsmiUserManage_Click(object sender, EventArgs e)
        {
            frmUserManage newFrmUserManage = new frmUserManage();
            newFrmUserManage.ShowDialog();
        }

        private void tsmiPermissionConfig_Click(object sender, EventArgs e)
        {
            frmPermissionConfig newFrmPermissionConfig = new frmPermissionConfig();
            newFrmPermissionConfig.ShowDialog();
        }

        private void tsbtnPrint_qd_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                //检查字段是否存在
                if (!CheckFieldExist("合同号|型号|尺寸|特性|电压|容量|执行标准|验收方式|已验数量|提交数量|筛选数量|淘汰率|生产日期|试验数量|DPA数量|验收方式|提交时间"))
                {
                    GeneralTools.ShowInfoMsg("请重新设置字段后再进行操作！");
                    return;
                }
                PrintListFrm newPrintListFrm = new PrintListFrm(dgv1.SelectedRows);
                newPrintListFrm.ShowDialog();
            }
        }

        private void tsbtnPrint_zl_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                //检查字段是否存在
                if (!CheckFieldExist("型号|尺寸|特性|电压|容量|技术条件|生产日期|批号"))
                {
                    GeneralTools.ShowInfoMsg("请重新设置字段后再进行操作！");
                    return;
                }
                ProductDocPrintFrm newProductDocPrintFrm = new ProductDocPrintFrm(dgv1.SelectedRows);
                newProductDocPrintFrm.ShowDialog();
            }
        }

        private void tsbtnExport_Click(object sender, EventArgs e)
        {
            string strTmpExportTemplate = frmExportTemplate.Show(ui);
            string[] strTmpFields=strTmpExportTemplate.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
            if (strTmpFields .Length ==0 )
            {
                GeneralTools.ShowInfoMsg("未选择有效的导出模板，数据导出失败！");
                return;
            }
            List<string> ExportFields = new List<string>();
            ExportFields.AddRange(strTmpFields);
            if (!CheckFieldExist(strTmpExportTemplate))
            {
                if (MessageBox.Show("选中的模板中有字段不存在，忽略错误跳过不存在的字段继续导出么？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel )
                {
                    GeneralTools.ShowInfoMsg("操作已取消！");
                    return;
                }
                for(int i=ExportFields .Count -1;i>=0;i--)
                {
                    if (dgv1.Columns[ExportFields [i]] == null)
                    {
                        ExportFields.RemoveAt(i);
                    }
                }
            }
            if (ExportFields.Count == 0)
            {
                GeneralTools.ShowInfoMsg("没有有效的字段设置，操作将取消！");
                return;
            }
            //获取文件名
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "(*.xls)|*.xls";
            sfd.ShowDialog();
            if (sfd.FileName.Length > 0)
            {
                if (dgv1.SelectedRows.Count > 0)
                {
                    //获取数据
                    DataTable tmpdt = new DataTable();
                    for (int i = 0; i < ExportFields.Count; i++)
                    {
                        tmpdt.Columns.Add(ExportFields[i]);
                    }
                    for (int i = dgv1.SelectedRows.Count - 1; i >= 0; i--)
                    {

                        DataRow r = tmpdt.NewRow();
                        for (int k = 0; k < ExportFields.Count; k++)
                        {
                            r[ExportFields[k]] = dgv1.SelectedRows[i].Cells[ExportFields[k]].Value;
                        }
                        tmpdt.Rows.Add(r);
                    }
                    if (ExportToExcel(sfd.FileName,tmpdt))
                    {
                        GeneralTools.ShowInfoMsg("Excel文件导出成功！");
                    }
                    else
                    {
                        GeneralTools .ShowErrorMsg ("Excel文件导出失败！");
                    }
                }
                else
                {
                    GeneralTools.ShowWarningMsg("请先选择要导出的记录！");
                }
            }
            
        }

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        private bool ExportToExcel(string strTmpFileName, DataTable tmpdt)
        {
            if (tmpdt==null )
            {
                return false;
            }
            IntPtr ip=new IntPtr ();
            try
            {
                Excel._Application tmpapp = new Excel.Application();
                ip =new IntPtr ( tmpapp.Hwnd);
                Excel.Workbook tmpwb = tmpapp.Workbooks.Add(true);
                Excel.Worksheet tmpws = (Excel.Worksheet)tmpwb.Worksheets[1];
                Excel.Range tmprange;
                object[] objHead = new object[tmpdt.Columns.Count];
                //列头
                for (int i = 0; i < tmpdt.Columns.Count; i++)
                {
                    objHead[i] = tmpdt.Columns[i].ColumnName;
                }
                tmprange = tmpws.get_Range("A1", tmpws.Cells[1, tmpdt.Columns.Count]); 
                tmprange.Value2 = objHead;
                //内容
                if (tmpdt.Rows.Count > 0)
                {
                    object[,] objBody = new object[tmpdt.Rows.Count, tmpdt.Columns.Count];
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        for (int k = 0; k < tmpdt.Columns.Count; k++)
                        {
                            objBody[i, k] = tmpdt.Rows[i][k];
                        }
                    }
                    tmprange = tmpws.get_Range("A2", tmpws.Cells[tmpdt.Rows.Count + 1, tmpdt.Columns.Count]);
                    tmprange.Value2 = objBody;
                }
                //保存
                tmpwb.SaveAs(strTmpFileName, Excel.XlFileFormat.xlExcel7, null, null, null, null, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                tmpapp.Quit();
                if (tmpapp != null)
                {
                    //IntPtr t = new IntPtr(tmpapp.Hwnd);
                    int k = 0;
                    GetWindowThreadProcessId(ip, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if ((int)ip!=0)
                {
                    //IntPtr t = new IntPtr(tmpapp.Hwnd);
                    int k = 0;
                    GetWindowThreadProcessId(ip, out k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill();
                }
            }
        }

        private void lstCondition_DoubleClick(object sender, EventArgs e)
        {
            if (lstCondition.SelectedIndex >= 0)
            {
                lstCondition.Items.RemoveAt(lstCondition.SelectedIndex);
                if (chkAutoQuery.Checked)
                {
                    DoQuery();
                }
            }
        }

        private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataTable tmpdt = (DataTable)dgv1.DataSource;
            tmpdt.Rows[e.RowIndex][e.ColumnIndex] = dgv1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            tmpdt.Rows[e.RowIndex].EndEdit();
        }

        private void tsbtnFirst_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[0].Cells[0];
            }
        }

        private void tsbtnLast_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[dgv1 .Rows.Count -1].Cells[0];
            }
        }

        private void tsbtnConfirm_ywc_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                if (CheckFieldExist("已完成"))
                {
                    //获取要查询的表的表名称
                    string strTmpTableName = string.Empty;
                    switch (tsiTableName.Text)
                    {
                        case "2008年":
                            strTmpTableName = "QZKFC_2008";
                            break;
                        case "2009年":
                            strTmpTableName = "QZKFC_2009";
                            break;
                        case "2009年之后":
                            strTmpTableName = "QZKFC";
                            break;
                    }
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateYwc.Replace("@表名称", strTmpTableName)
                                                                                                .Replace("@已完成", "1")
                                                                                                .Replace("@序号", GeneralTools.GetString(dgv1.SelectedRows[i].Cells["序号"].Value, string.Empty)));
                    }
                    DoQuery();
                }
                else
                {
                    GeneralTools.ShowInfoMsg("'已完成'字段未选择，请设置字段后重新操作！");
                    return;
                }
            }
        }


        private void tsbtnConfirm_wwc_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                if (CheckFieldExist("已完成"))
                {
                    //获取要查询的表的表名称
                    string strTmpTableName = string.Empty;
                    switch (tsiTableName.Text)
                    {
                        case "2008年":
                            strTmpTableName = "QZKFC_2008";
                            break;
                        case "2009年":
                            strTmpTableName = "QZKFC_2009";
                            break;
                        case "2009年之后":
                            strTmpTableName = "QZKFC";
                            break;
                    }
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateYwc.Replace("@表名称", strTmpTableName)
                                                                                                .Replace("@已完成", "0")
                                                                                                .Replace("@序号", GeneralTools.GetString(dgv1.SelectedRows[i].Cells["序号"].Value, string.Empty)));
                    }
                    DoQuery();
                }
                else
                {
                    GeneralTools.ShowInfoMsg("'已完成'字段未选择，请设置字段后重新操作！");
                    return;
                }
            }
        }

        private void tsbtnConfirm_ykp_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                if (CheckFieldExist("已开票|开票日期"))
                {
                    //获取要查询的表的表名称
                    string strTmpTableName = string.Empty;
                    switch (tsiTableName.Text)
                    {
                        case "2008年":
                            strTmpTableName = "QZKFC_2008";
                            break;
                        case "2009年":
                            strTmpTableName = "QZKFC_2009";
                            break;
                        case "2009年之后":
                            strTmpTableName = "QZKFC";
                            break;
                    }
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateKp .Replace("@表名称", strTmpTableName )
                                                                                                .Replace("@已开票", "已开票")
                                                                                               .Replace ("@开票日期","GetDate()")
                                                                                                .Replace("@序号", GeneralTools.GetString(dgv1.SelectedRows[i].Cells["序号"].Value, string.Empty)));
                    }
                    DoQuery();
                }
                else
                {
                    GeneralTools.ShowInfoMsg("'已开票'字段或'开票日期'字段未选择，请设置字段后重新操作！");
                    return;
                }
            }
        }


        private void tsbtnConfirm_wkp_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                if (CheckFieldExist("已开票|开票日期"))
                {
                    //获取要查询的表的表名称
                    string strTmpTableName = string.Empty;
                    switch (tsiTableName.Text)
                    {
                        case "2008年":
                            strTmpTableName = "QZKFC_2008";
                            break;
                        case "2009年":
                            strTmpTableName = "QZKFC_2009";
                            break;
                        case "2009年之后":
                            strTmpTableName = "QZKFC";
                            break;
                    }
                    for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateKp.Replace("@表名称", strTmpTableName)
                                                                                                .Replace("@已开票", "未开票")
                                                                                               .Replace("@开票日期", "NULL")
                                                                                                .Replace("@序号", GeneralTools.GetString(dgv1.SelectedRows[i].Cells["序号"].Value, string.Empty)));
                    }
                    DoQuery();
                }
                else
                {
                    GeneralTools.ShowInfoMsg("'已开票'字段或'开票日期'字段未选择，请设置字段后重新操作！");
                    return;
                }
            }
        }

        private void tsmiCopy_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedCells.Count > 0)
            {
                DataGridClipBoard = dgv1.SelectedCells;
            }
        }

        private void tsmiPaste_Click(object sender, EventArgs e)
        {
            if (DataGridClipBoard != null && dgv1.SelectedCells.Count > 0)
            {
                foreach (DataGridViewCell dgvc in dgv1.SelectedCells)
                {
                    foreach (DataGridViewCell dgvc2 in DataGridClipBoard)
                    {
                        if (dgvc.ColumnIndex == dgvc2.ColumnIndex)
                        {
                            dgvc.Value = dgvc2.Value;
                            break;
                        }
                    }
                }
                dgv1.EndEdit();
            }
        }

        private void dgv1_DoubleClick(object sender, EventArgs e)
        {
            if (tsbtnEdit.Enabled)
            {
                tsbtnEdit_Click(this, new EventArgs());
            }
        }

        private void tsmiChangePassword_Click(object sender, EventArgs e)
        {
            frmChangePassword newFrmChangePassword = new frmChangePassword(ui.UserName);
            newFrmChangePassword.ShowDialog();
        }

        private void tsmiClear_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewCell dgvc in dgv1.SelectedCells)
            {
                dgvc.Value = DBNull .Value ;
            }
        }

        /// <summary>
        /// 检查是否符合2.5UR筛选条件
        /// </summary>
        /// <param name="strTmpModel">型号</param>
        /// <param name="strTmpSize">尺寸</param>
        /// <param name="strTmpVoltage">电压</param>
        /// <param name="strTmpCapacity">容量</param>
        /// <param name="strTmpSN">批号</param>
        /// <returns>是/否</returns>
        private bool CheckJsSxVoltage(string strTmpModel, string strTmpSize, string strTmpVoltage, string strTmpCapacity, string strTmpSN)
        {
            try
            {
                double dVoltage = VoltageTranslate(strTmpVoltage);
                double dCapacity = CapactiyTranslate(strTmpCapacity);
                if (GetDouble(strTmpSN.Substring(0, 4)) >= 1608)
                {
                    #region 2016年8月以后、2018年3月以前的批号，按老范围判断
                    //高频产品
                    if (strTmpModel.StartsWith("CC"))
                    {
                        if (dVoltage == 25)
                        {
                            #region 高频25V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 470)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 2200)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                    if (dCapacity <= 10000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                case "1210":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                    if (dCapacity <= 150000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                    if (dCapacity <= 220000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3035":
                                case "4045":
                                    if (dCapacity <= 330000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "5868":
                                    if (dCapacity <= 680000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 50)
                        {
                            #region 高频50V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 470)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 1500)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                    if (dCapacity <= 6800)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 68000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 47000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                    if (dCapacity <= 150000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3035":
                                case "4045":
                                    if (dCapacity <= 220000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "5868":
                                    if (dCapacity <= 470000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 100)
                        {
                            #region 高频100V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 100)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 1000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                    if (dCapacity <= 3300)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 33000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 10000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                    if (dCapacity <= 68000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                    if (dCapacity <= 82000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3035":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                    if (dCapacity <= 150000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                    break;
                                case "5868":
                                    if (dCapacity <= 330000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else
                        {
                            return false;
                        }
                    }
                    //低频产品
                    else
                    {
                        if (dVoltage == 6.3 || dVoltage == 10 || dVoltage == 16 || dVoltage == 25)
                        {
                            #region 低频6.3V/10V/16V/25V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 47000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                case "1":
                                    if (dCapacity <= 150000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 1000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 330000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                    if (dCapacity <= 1500000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                    if (dCapacity <= 2200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                    if (dCapacity <= 3300000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                    if (dCapacity <= 4700000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                    if (dCapacity <= 10000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "5868":
                                    if (dCapacity <= 15000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 50)
                        {
                            #region 低频50V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 10000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 47000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 680000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 220000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                    if (dCapacity <= 1000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                    if (dCapacity <= 1500000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                    if (dCapacity <= 2200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                    if (dCapacity <= 3300000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                    if (dCapacity <= 6800000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "5868":
                                    if (dCapacity <= 10000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 100)
                        {
                            #region 低频100V
                            switch (strTmpSize)
                            {
                                case "0603":
                                    if (dCapacity <= 15000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                    if (dCapacity <= 47000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 330000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                    if (dCapacity <= 680000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                    if (dCapacity <= 1000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                    if (dCapacity <= 1500000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                    if (dCapacity <= 2200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                    if (dCapacity <= 3300000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "5868":
                                    if (dCapacity <= 4700000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else
                        {
                            return false;
                        }
                    }
                    #endregion
                }
                else if (1 > 2)
                {
                    #region 2018年4月以后的批号按新范围处理
                    //高频产品
                    if (strTmpModel.StartsWith("CC"))
                    {
                        if (dVoltage == 25)
                        {
                            #region 高频25V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 270)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 1000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                case "1":
                                    if (dCapacity <= 4700)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 12000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 27000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                case "3a":
                                    if (dCapacity <= 47000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                case "3b":
                                    if (dCapacity <= 56000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3c":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                case "4":
                                    if (dCapacity <= 270000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                case "5":
                                    if (dCapacity <= 330000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "5868":
                                case "6":
                                    if (dCapacity <= 820000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 50)
                        {
                            #region 高频50V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 220)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 820)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                case "1":
                                    if (dCapacity <= 3300)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 8200)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 15000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;

                                case "1812":
                                case "3a":
                                    if (dCapacity <= 33000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                case "3b":
                                    if (dCapacity <= 47000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3c":
                                    if (dCapacity <= 68000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                case "4":
                                    if (dCapacity <= 220000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                case "5":
                                    if (dCapacity <= 270000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "5868":
                                case "6":
                                    if (dCapacity <= 680000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 100)
                        {
                            #region 高频100V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 100)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 680)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                case "1":
                                    if (dCapacity <= 1800)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 4700)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 8200)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                case "3a":
                                    if (dCapacity <= 22000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                case "3b":
                                    if (dCapacity <= 33000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3c":
                                    if (dCapacity <= 56000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                case "4":
                                    if (dCapacity <= 150000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                case "5":
                                    if (dCapacity <= 220000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                    break;
                                case "5868":
                                case "6":
                                    if (dCapacity <= 560000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else
                        {
                            return false;
                        }
                    }
                    //低频产品
                    else
                    {
                        if (dVoltage == 6.3 || dVoltage == 10 || dVoltage == 16 || dVoltage == 25)
                        {
                            #region 低频6.3V/10V/16V/25V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 4700)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 56000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                case "1":
                                    if (dCapacity <= 150000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 220000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 1000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                case "3a":
                                    if (dCapacity <= 1200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                case "3b":
                                    if (dCapacity <= 1500000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3c":
                                    if (dCapacity <= 2200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                case "4":
                                    if (dCapacity <= 3300000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                case "5":
                                    if (dCapacity <= 47000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 50)
                        {
                            #region 低频50V
                            switch (strTmpSize)
                            {
                                case "0402":
                                    if (dCapacity <= 4700)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0603":
                                    if (dCapacity <= 33000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                case "1":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 120000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 330000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                case "3a":
                                    if (dCapacity <= 1000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                case "3b":
                                case "2225":
                                case "3c":
                                    if (dCapacity <= 1200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                case "4":
                                    if (dCapacity <= 1500000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                case "5":
                                    if (dCapacity <= 2200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else if (dVoltage == 100)
                        {
                            #region 低频100V
                            switch (strTmpSize)
                            {
                                case "0603":
                                    if (dCapacity <= 10000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "0805":
                                case "1":
                                    if (dCapacity <= 47000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1206":
                                    if (dCapacity <= 68000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1210":
                                    if (dCapacity <= 100000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "1812":
                                case "3a":
                                    if (dCapacity <= 470000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2220":
                                case "3b":
                                    if (dCapacity <= 680000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "2225":
                                case "3c":
                                    if (dCapacity <= 1000000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "3035":
                                case "4":
                                    if (dCapacity <= 1200000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                case "4045":
                                case "5":
                                    if (dCapacity <= 1500000)
                                    {
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                    break;
                                default:
                                    return false;
                            }
                            #endregion
                        }
                        else
                        {
                            return false;
                        }
                    }
                    #endregion
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        private double GetDouble(object tmpobj)
        {
            try
            {
                return Convert.ToDouble(tmpobj);
            }
            catch
            {
                return 0d;
            }
        }

        private double CapactiyTranslate(string strC)
        {
            string strTmp = string.Empty;
            //如果容量中包含PF字样，如195PF±0.5PF或16500PFJ
            if (strC.Contains("PF"))
            {
                strTmp = strC.Substring(0, strC.IndexOf('P'));
                return GetDouble(strTmp);
            }
            //如果未包含PF字样，则一律按照三标法处理
            try
            {
                strTmp = strC.Substring(0, 3).ToUpper();
                if (strTmp.Contains("R"))
                {
                    return GetDouble(strTmp.Replace("R", "."));
                }
                else
                {
                    double iX = Convert.ToDouble(strTmp.Substring(0, 2));
                    double iY = Convert.ToDouble(strTmp.Substring(2, 1));
                    return iX * Math.Pow(10, iY);
                }
            }
            catch
            {
                return 0d;
            }
        }

        private double VoltageTranslate(string strTmpVoltage)
        {
            double r = 0d;
            if (strTmpVoltage.ToUpper().Contains("V"))
            {
                strTmpVoltage = strTmpVoltage.Replace("V", "");
            }
            if (strTmpVoltage.ToUpper().Contains("K"))
            {
                r = GetDouble(strTmpVoltage.Substring(0, strTmpVoltage.Length - 1)) * 1000;
            }
            else
            {
                r = GetDouble(strTmpVoltage);
            }
            return r;
        }
    }
}